$(document).ready(
		function(){
			checkDislay();
			// Xem lich su choi:
			$("#_k_member_view_history").click(function(e) {
				$("#_k_dialog_info").dialog({
					height: 360,
			      	width: 260,
			      	modal: true,
					open: function(e,u){
						$.ajax({
							url: "last_function.php",
							type: "POST",
							dataType: "json",
							success: function(res){
								var current = res.current[0];
								$("#_k_info_1").text(current.so_luot_da_choi);
								$("#_k_info_2").text(current.ngay_tham_gia);
								$("#_k_info_3").text(current.total_point);
								$("#_k_info_4").text(current.so_luot_da_choi);
							}
						});
					},
					buttons:{
				        "OK": function() {
				            $( this ).dialog( "close" );
				          }
			        }
				});				
			} );
			// Click nut gui mail
			$("#_k_sendmail_button").click(
					function(e){
						e.preventDefault();
						var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						var email  = $("#_k_verify_email").val();
						var phoneno  = $("#_k_verify_phoneno").val();
						if(email == '' || phoneno == ''){
							alert("Hãy điền đầy đủ thông tin.");
							return;
						}
						if(!regex.test(email)){
							alert("Hãy điền vào email hợp lệ.");return;
						}
						$("#_k_loading").show();
						$.ajax({
							url: '_admin/include/mail.php?email=' + email + "&phoneno=" + phoneno,
							type: 'GET',
							dataType: "json",
							success: function(res){
								if(res.result == 'successful'){
									//End show loading
									$("#_k_loading").hide();
									
									$("#_k_verify_member>form>input").hide();
									$("#_k_verify_msg").html("<b style='color:green'>Gửi mail thành công, hãy check mail (kiểm tra trong spam).</b>");
								}
								else{
									$("#_k_verify_msg").html("<b style='color:red'>Gửi mail không thành công, xem lại email.</b>");
								}
							},
							error: function(res){
								$("#_k_verify_msg").html("<b style='color:red'>Có lỗi hệ thống.</b>");
							}
						});
						
					}
			);
			//End
			
			// xu li click vao cac hien vat
			var stars = ["star1", "star2", "star3"];
			var flags = ["flag1", "flag2", "flag3", "flag4"];
//		    var status = ["true", "true", "true", "true"];
			$("._k_item").click(function(e){
				$.ajax({
					url: "play.php",
					type: "POST",
					dataType: "json",
					success: function(response){
						var res = response.result;
						for (var i = 0; i < stars.length; i++) {
							if (e.target.id == stars[i]) {
								var phaohoa = new Audio("sound/phaohoa.mp3");
								phaohoa.play();
								$("#content .game_area span#firework" + (i + 1)).css("display", "block");
							}
						}
						for (var i = 0; i < flags.length; i++) {
							if (e.target.id == flags[i]){//&& status[i] == "true") {
								$("#content .game_area span.flag").css({"opacity": "1" });
								var flagSound = new Audio("sound/FlagSounds.mp3");
								flagSound.play();
//				                $("#content .game_area span#flag" + (i + 1)).css({"background": "url('images/flag.png') no-repeat",
//				                                                                    "background-size": "contain"
//				                                                                    });
//				                status[i] = "false";
							}
						}
						if(res == -1){
							var ohoh = new Audio("sound/OhOh.mp3");
							ohoh.play();
							showTurnOff(e);
							checkDislay();
							$("#content .game_area span.flag").css({"opacity": "0.3" });
							$("#_k_dialog_invite").dialog({
								height: 200,
								width: 400,
								modal: true,
								open: function(){
									$.get("get_ref_link.php",{},function(data){
										$("#_k_reflink").css("width","300px").val(data);
									});
								}
							});
							return;
						}
						if(0 <= res <= 50000){
							//showScore(e, res);
							// Show dialog
							showScoreDialog(res,response.luotchoiconlai);
						}
						// Update diem
						
						checkDislay();				
					},
					error: function(res){
						alert(Object.keys(res));
					}
				});
			});
		}
);

function checkDislay(){
	$.ajax({
		url: "songuoichoi_danhsachtop.php",
		dataType: "json",
		type: "POST",
		success: function(res){
			// display count
			$("#_k_songuoichoi").text(res.count);
			//Display top nguoi choi:
			var objNguoichoiList = res.top;
			var size = objNguoichoiList.length;
			$("#_k_top_nguoi_choi").html("");
			for(var i = 0; i < size; i++){
				var tmp = objNguoichoiList[i];
				if(tmp.name.length > 20) 
					tmp.name = tmp.name.substring(0,20) + "...";
				$("#_k_top_nguoi_choi").append(
						"<li>"+tmp.name.substring(0,16)+"..........<span class=\"total_score\">"+tmp.total_point+"</span> điểm</li>"
				);
			}
			// Display current
			var current = res.current[0];
			$("#_k_member_view_history").text(current.name);
			$("#k_turnn").text("" + current.so_luot_da_choi);
			$("#k_diemm").text(""+current.total_point);
//			var dom = $("#_k_current_player");
//			dom.html(
//					"<span>Name:</span><span class=\"player\" id=\"_k_current_playername\">"+current.name+"</span> | Lượt chơi: <span></span>"+
//                    "<span class=\"turn\">"+current.so_luot_da_choi+"</span> | Điểm: <span></span>"+
//                    "<span class=\"score\">"+current.total_point+"</span>"
//			);
		},
		error: function(res){
			alert("Loi update screen");
		}
	});
}
function confirmThoat(){
	return confirm("Bạn có muốn thoát khỏi ứng dụng?");
	return false;
}
//----
function showScoreDialog(diem,luotchoiconlai){
	$("#_k_dialog_for_click_event").dialog({
		height: 260,
      	width: 500,
      	modal: true,
      	open: function(event, ui){
      		$("#_k_dialog_for_click_event_diem").text("" + diem);
      		$("#_k_dialog_for_click_event_remain").text("" + luotchoiconlai);
      	},
      	close: function(ev, ui) { 
      		$("#content .game_area span.flag").css({"opacity": "0.3" }); 
      	}
	});
//	$("#content .game_area span#flag" + (i + 1)).css({"opacity": "1" });
}

//----

function showTurnOff(e) {
    $(".scoreplus").css({
            "top": e.pageY - 50,
            "left": e.pageX - 50,
            "background": "url('images/turnoff.png') no-repeat",
            "background-size": "contain",
            "display": "block"
        });
    $(".scoreplus").animate({top: (e.pageY - 100) + 'px'},"slow");
    $(".scoreplus").fadeOut(200);
}
function showScore(e, data) {
    $(".scoreplus").css({
            "top": e.pageY - 50,
            "left": e.pageX - 50,
            "background": "url('images/" + data +".png') no-repeat",
            "background-size": "contain",
            "display": "block"
        });
    $(".scoreplus").animate({top: (e.pageY - 100) + 'px'},"slow");
    $(".scoreplus").fadeOut(500);
}